package com.my.security.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CustomUserDetailsService implements UserDetailsService {
//    @Autowired
//    private UserInfoService userInfoService;
    /**
     * 需新建配置类注册一个指定的加密方式Bean，或在下一步Security配置类中注册指定
     */
    //security自定义的实现类
    //官方推荐BCryptPasswordEncoder hash算法单向加密 默认长度10位
    @Autowired
    private PasswordEncoder pw;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 通过用户名从数据库获取用户信息
//        UserInfo userInfo = userInfoService.getUserInfo(username);
//        if (userInfo == null) {
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        String passWord = pw.encode("123");
//        // 得到用户角色
//        String role = userInfo.getRole();
//        // 角色集合
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        // 角色必须以`ROLE_`开头，数据库中没有，则在这里加
//        authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
        List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList
                ("admin,normal,ROLE_abc,/main.html");
//        ROLE_abc 定义了一个角色abc  ROLE不能少

        //User security自定义的实现类
        return new User(
//                userInfo.getUsername(),
                "admin",
                // 因为数据库是明文，所以这里需加密密码
                passWord,
//                passwordEncoder.encode(userInfo.getPassword()),
//                authorities
                grantedAuthorities
        );
    }
}
